//
//  DHOrderReturnView.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/1/21.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHOrderReturnView.h"
#import "DHOrder_ReturnCell.h"

@interface DHOrderReturnView()<UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) UILabel *noDataLabel;
@property (strong, nonatomic) NSArray *returnArray;
@property (strong, nonatomic) UITableView *tableView;

@end

@implementation DHOrderReturnView

- (NSArray *)returnArray{
    if (!_returnArray) {
        _returnArray = [NSArray array];
    }
    return _returnArray;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        if (!self.tableView) {
            self.tableView = [[UITableView alloc] init];
            self.tableView.delegate = self;
            self.tableView.dataSource = self;
            [self.tableView registerNib:[UINib nibWithNibName:@"DHOrder_ReturnCell" bundle:nil] forCellReuseIdentifier:kDHOrder_ReturnCell];
            [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
            self.tableView.mj_header = [DHMJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadReturns)];
            [self addSubview:self.tableView];
            [self.tableView.mj_header beginRefreshing];
        }
        if (!self.noDataLabel) {
            self.noDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 50)];
            self.noDataLabel.textAlignment = NSTextAlignmentCenter;
            self.noDataLabel.font = [UIFont systemFontOfSize:16];
            self.noDataLabel.textColor = [UIColor colorWithHexString:@"#4a4a4a"];
            self.noDataLabel.text = @"暂无需要归还的服饰";
            [self addSubview:self.noDataLabel];
        }
        self.noDataLabel.hidden = YES;
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.tableView.frame = CGRectMake(0, 0, self.width, self.height);
}

- (void)loadReturns{
    postN(@"loadReturnData");
}

- (void)reloadDataWithReturnArray:(NSArray *)returnArray{
    [self.tableView.mj_header endRefreshing];
    self.returnArray = returnArray;
    if (self.returnArray.count == 0) {
        self.noDataLabel.hidden = NO;
    }else{
        self.noDataLabel.hidden = YES;
    }
    [self.tableView reloadData];
}

#pragma mark - UITableViewDataSource and UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.returnArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 136;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DHOrder_ReturnCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHOrder_ReturnCell forIndexPath:indexPath];
    NSDictionary *dicProduct = [self.returnArray objectAtIndex:indexPath.row];
    [cell updateCell:dicProduct];
    if ([self.selectedReturnArr containsObject:dicProduct]) {
        [cell.returnbutton setBackgroundColor:[UIColor colorWithHexString:@"#1A82FF"]];
        cell.returnbutton.selected = YES;

    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%ld",(long)indexPath.row);
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    self.noDataLabel.hidden = YES;
}

@end
